package com.example.hello.hello.base;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.hello.hello.R;


public abstract class BaseFragment extends Fragment implements View.OnClickListener {

    protected LinearLayout title;
    protected ImageButton mliftButton;
    protected ImageButton mrightButton;
    protected TextView textView;
    protected View view;

    @Override
    public View onCreateView(LayoutInflater inflater,  ViewGroup container,  Bundle savedInstanceState) {
        view = inflater.inflate(getLayoutId(), null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView(savedInstanceState);
    }

    public  void initTitleBar(){
        title = (LinearLayout) view.findViewById(R.id.l_title);
        if(title != null){
            title.setVisibility(View.VISIBLE);
            textView = (TextView)view.findViewById(R.id.tv_title);
            mliftButton = (ImageButton)view.findViewById(R.id.ib_titleback);
            mrightButton = (ImageButton) view.findViewById(R.id.ib_mytitle);
            mliftButton.setOnClickListener(this);
            mrightButton.setOnClickListener(this);
        }
    }
    protected  int getLayoutId(){
        return 0;
    }

    public void onClick(View v){
        switch (v.getId()){
            case R.id.ib_titleback:
                onClickLeftBtn();
                break;
            case R.id.ib_mytitle:
                onClickReghtBtn();
                break;
        }
    }
    /**
     * 设置中间文本
     */
    protected void setMidTxt(String strTxt) {
        if (textView != null) {
            textView.setText(strTxt);
        }
    }
    protected void setMidTxtbyInt(int strTxt) {
        if (textView != null) {
            textView.setText(strTxt);
        }
    }
    protected void setLeftVisibility(Boolean isVisibility){
        if(isVisibility){
            mliftButton.setVisibility(View.VISIBLE);
        }else{
            mliftButton.setVisibility(View.GONE);
        }

    }
    protected void setRightVisibility(Boolean isVisibility){
        if(isVisibility){
            mrightButton.setVisibility(View.VISIBLE);
        }else{
            mrightButton.setVisibility(View.GONE);
        }
    }

    protected void setRightImg(int img){
        if(img != 0){
            mrightButton.setBackgroundResource(img);
        }
    }
    protected void setRightImg(Drawable img){
        if(img !=null){
            mrightButton.setImageDrawable(img);
        }
    }
    protected abstract void onClickLeftBtn();
    protected abstract void onClickReghtBtn();
    /**
     * 初始化页面
     * @param savedInstanceState
     */
    protected abstract void initView(Bundle savedInstanceState);
}
